package com.jd.sunny.java.multithreading.concurrent.threadpool;

/**
 * @Description:default ExecutorService implement , is a thread pool
 * @author:sunny
 * @since:2012-12-25 16:06
 * @version:1.0.0
 */

import java.util.concurrent.*;

/**
 * 1.public interface ExecutorService extends Executor
 * 2.public abstract class AbstractExecutorService implements ExecutorService
 * 3.public class ThreadPoolExecutorextends AbstractExecutorService
 *
 *
 */
public class ExcutorExcutorServiceThreadPoolExecutorTest implements Executor {

    /**
     * excute the runnable
     * @param runnable
     */
    public void execute(Runnable runnable) {
        new Thread(runnable).start();
    }

    public static void main(String[] args){

        /**Creates a thread pool that reuses a fixed number of threads
         *return is a ThreadPoolExecutor object
         public static ExecutorService newFixedThreadPool(int nThreads) {
         return new ThreadPoolExecutor(nThreads, nThreads,
         0L, TimeUnit.MILLISECONDS,
         new LinkedBlockingQueue<Runnable>());
         }
         */
        //Use Executors class to get a ThreadPoolExecutor object
        // default runnable queue is  new LinkedBlockingQueue<Runnable>()
        ExecutorService executorService =  Executors.newFixedThreadPool(5);
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;

        // run a thread
        threadPoolExecutor.submit(new Runnable() {
            public void run() {
                int count = 0;
                while ((count < 10000) && (!Thread.currentThread().isInterrupted())){
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println("Thread is running" + count);
                    count++;
                }

            }
        });

        // shutdown the thread pool after complete the task
//        threadPoolExecutor.shutdown();

        //shutdown the pool now ,this method call Thread.interupt();
        // so ,the thread is not shutdown immediately
        // and we must judge when to interupt the thread
        //even some time ,the interupt maybe fail and throw Exception,like when the thread is sleep
        threadPoolExecutor.shutdownNow();


        //new a ThreadPoolExecutor myself
        //param: min thread count,max thread count ,
        // when the current thread count > min thread count recycle the thread wait time ,the runnable queue
        ThreadPoolExecutor myThreadPoolExecutor = new ThreadPoolExecutor(10,30,10,
                TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
        myThreadPoolExecutor.execute(new Runnable() {
            public void run() {
                int count = 0;
                while ((count < 100)){
                    System.out.println("Thread is running" + count);
                    count++;
                }
            }
        });


        
    }

}
